package his;

import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.ExecWatch;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.util.Config;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author fachang.mao
 * @Date 2024/12/11 23:04
 * @Version 1.0
 */
public class Test1 {

    private static void getOpsExecWatch() {
        try (KubernetesClient client = new DefaultKubernetesClient()) {
            String podName = "crm-publiccloud-5fcdb4749b-rlr8s";
            String namespace = "default";
            String containerName = "crm";
            String[] command = {"/bin/sh", "-c", "echo Hello, World!"};

            final CountDownLatch latch = new CountDownLatch(1);
            final StringBuilder stdoutBuilder = new StringBuilder();
            final StringBuilder stderrBuilder = new StringBuilder();
            final Lock lock = new ReentrantLock();

            // 创建自定义 OutputStream 用于捕获输出
            OutputStream captureOutput = new OutputStream() {
                @Override
                public void write(int b) throws IOException {
                    lock.lock();
                    try {
                        stdoutBuilder.append((char) b);
                    } finally {
                        lock.unlock();
                    }
                }

                @Override
                public void write(byte[] b, int off, int len) throws IOException {
                    lock.lock();
                    try {
                        stdoutBuilder.append(new String(b, off, len));
                    } finally {
                        lock.unlock();
                    }
                }
            };

            // 创建自定义 OutputStream 用于捕获错误
            OutputStream captureError = new OutputStream() {
                @Override
                public void write(int b) throws IOException {
                    lock.lock();
                    try {
                        stderrBuilder.append((char) b);
                    } finally {
                        lock.unlock();
                    }
                }

                @Override
                public void write(byte[] b, int off, int len) throws IOException {
                    lock.lock();
                    try {
                        stderrBuilder.append(new String(b, off, len));
                    } finally {
                        lock.unlock();
                    }
                }
            };

            // 创建 ExecWatch 并设置回调来捕获输出和错误信息
            ExecWatch execWatch = client.pods()
                    .inNamespace(namespace)
                    .withName(podName)
                    .inContainer(containerName)
                    .writingOutput(captureOutput)
                    .writingError(captureError)
                    .exec(command);

            // 等待直到所有的输出被处理完
            latch.await();

            // 关闭 ExecWatch
            execWatch.close();

            System.out.println("Standard Output: " + stdoutBuilder.toString());
            System.out.println("Standard Error: " + stderrBuilder.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        getOpsExecWatch();
    }

}
